package com.icloud.wallet.boot.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.icloud.common.core.api.ResultCode;
import com.icloud.common.web.exception.ApiException;
import com.icloud.common.web.vo.PageVO;
import com.icloud.common.database.enums.CryptoCurrencyEnum;
import com.icloud.common.database.enums.WithdrawStateEnum;
import com.icloud.common.database.mapper.TOrderMapper;
import com.icloud.wallet.boot.mapper.TRechargeOrderItemMapper;
import com.icloud.common.database.mapper.TWalletJournalMapper;
import com.icloud.wallet.boot.mapper.TWithdrawOrderItemMapper;
import com.icloud.common.database.model.TOrders;
import com.icloud.wallet.boot.service.ITOrderQueryService;
import com.icloud.wallet.boot.dto.RechargeOrderPageDto;
import com.icloud.wallet.boot.vo.RechargeOrderVO;
import com.icloud.wallet.boot.dto.WithdrawOrderPageDto;
import com.icloud.wallet.boot.vo.WithdrawOrderVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Slf4j
@Service
public class TOrderQueryServiceImpl extends ServiceImpl<TOrderMapper, TOrders> implements ITOrderQueryService {
    @Autowired
    private TWalletJournalMapper tWalletJournalMapper;
    @Autowired
    private TRechargeOrderItemMapper tRechargeOrderItemMapper;
    @Autowired
    private TWithdrawOrderItemMapper tWithdrawOrderItemMapper;
    @Autowired
    private TOrderMapper tOrderMapper;

    @Override
    public PageVO<RechargeOrderVO> rechargeOrderPage(RechargeOrderPageDto dto) {
        if(StringUtils.isNotBlank(dto.getCryptoCurrency())) {
            CryptoCurrencyEnum cryptoCurrencyEnum = CryptoCurrencyEnum.byCode(dto.getCryptoCurrency());
            if (cryptoCurrencyEnum == null) {
                throw new ApiException(ResultCode.ILLEGAL_CRYPTO_CURRENCY);
            }
        }

        Long current = dto.getCurrent();
        Long pageSize = dto.getPageSize();
        Long countAll = 0l;
        List<RechargeOrderVO> rechargeOrderVOList = null;
        if(current!=null && current>0 && pageSize!=null && pageSize>0){
            Page<RechargeOrderVO> page = Page.of(current, pageSize);
            IPage<RechargeOrderVO> rechargeOrderVOPage = tRechargeOrderItemMapper.findAll(page, dto);
            rechargeOrderVOList = rechargeOrderVOPage.getRecords();
            countAll = page.getTotal();
        }else{
            countAll = tRechargeOrderItemMapper.countAll(dto);
            rechargeOrderVOList = tRechargeOrderItemMapper.findAll(dto);
        }

        rechargeOrderVOList.forEach(recharge->{
        });

        PageVO<RechargeOrderVO> pageVO = new PageVO<>();
        pageVO.setTotal(countAll);
        if(CollectionUtils.isEmpty(rechargeOrderVOList)){
            pageVO.setList(List.of());
        }else {
            pageVO.setList(rechargeOrderVOList);
        }
        return pageVO;
    }

    @Override
    public PageVO<WithdrawOrderVO> withdrawOrderPage(WithdrawOrderPageDto dto) {
        if(StringUtils.isNotBlank(dto.getCryptoCurrency())) {
            CryptoCurrencyEnum cryptoCurrencyEnum = CryptoCurrencyEnum.byCode(dto.getCryptoCurrency());
            if (cryptoCurrencyEnum == null) {
                throw new ApiException(ResultCode.ILLEGAL_CRYPTO_CURRENCY);
            }
        }
        if(StringUtils.isNotBlank(dto.getWithdrawState())) {
            WithdrawStateEnum handleStateEnum = WithdrawStateEnum.byCode(dto.getWithdrawState());
            if (handleStateEnum == null) {
                throw new ApiException(ResultCode.ILLEGAL_HANDLE_STATE);
            }
        }

        Long current = dto.getCurrent();
        Long pageSize = dto.getPageSize();
        Long countAll = 0l;
        List<WithdrawOrderVO> withdrawOrderVOList = null;
        if(current!=null && current>0 && pageSize!=null && pageSize>0){
            Page<WithdrawOrderVO> page = Page.of(current, pageSize);
            IPage<WithdrawOrderVO> subscribeOrderVOPage = tWithdrawOrderItemMapper.findAll(page, dto);
            withdrawOrderVOList = subscribeOrderVOPage.getRecords();
            countAll = page.getTotal();
        }else{
            countAll = tWithdrawOrderItemMapper.countAll(dto);
            withdrawOrderVOList = tWithdrawOrderItemMapper.findAll(dto);
        }

        withdrawOrderVOList.forEach(wo->{
        });

        PageVO<WithdrawOrderVO> pageVO = new PageVO<>();
        pageVO.setTotal(countAll);
        if(CollectionUtils.isEmpty(withdrawOrderVOList)){
            pageVO.setList(List.of());
        }else {
            pageVO.setList(withdrawOrderVOList);
        }
        return pageVO;
    }
}
